<?php
namespace app\admin\controller;
use think\Request;

class Xixiche extends Base
{
    // // 成功跳转
    // public function jump_suc($info, $url)
    // {
    //     $data['status'] = 1;
    //     $data['info'] = $info;
    //     $data['url'] = $url;
    //     $this->assign('data', $data);
    //     $this->fetch('xixiche/statuspage');
    //     die;
    // }

    // // 失败跳转
    // public function jump_err($info, $url)
    // {
    //     $data['status'] = 0;
    //     $data['info'] = $info;
    //     $data['url'] = $url;

    //     $this->assign('data', $data);
    //     return $this->fetch('xixiche/statuspage');
    // }

    // 首页框架
    public function index()
    {
        if ( ! session('?user_id')) {
            $this->redirect('admin/xixiche/login');
        }
        
        return $this->fetch();
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {

            $post = request()->only(['account', 'pwd'], 'post');

            if (empty($post['account'])) {
                err('请输入账号');
            }
            if (empty($post['pwd'])) {
                err('请输入密码');
            }
            
            $result = db('customer')->where(['username' => $post['account'],'id'=>1002])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();

            // 查找不到
            if(! $result) {
                err('该账号不存在');
            }
            
            if($result['status'] != 1) {
                err('账号已禁用，请联系管理员');
            }
 
            // 密码不正确
            if(md5($post['pwd']) != $result['pwd']) {
                err('密码错误，请重新输入');
            }

            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);

            // 验证成功
            suc('验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/xixiche/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }
    
    // 用户列表 - 工作人员 - 展示
    public function user_list_info()
    {
        $post = Request::instance()->param();
        $where['admin_id'] = $this->accountId;
        $where['member_status'] = 1;
        if($_GET)
        {
            if(!empty($post['nickname'])){
                $where['nickname'] = ['like','%'.$post['nickname'].'%'];
            }
        }

        $res = db('user')->order('id desc')->where($where)->paginate(20,false);
        $page = $res->render();
        $this->assign('page', $page);

        $nickname = !empty($post['nickname'])?$post['nickname']:'';
        $this->assign('nickname',$nickname);

        $this->assign('res', $res);
        return $this->fetch();
    }

    // 用户列表 - 普通用户 - 展示
    public function user_list()
    {
        $post = Request::instance()->param();

        if ($_GET) {
            if (!empty($post['nickname'])) {
                $where['nickname'] = ['like','%'.$post['nickname'].'%'];
            }
        }

        $where['admin_id'] = $this->accountId;
        $where['member_status'] = 0;

        $res = db('user')->order('id desc')->where($where)->paginate(20,false);

        $page = $res->render();
        $this->assign('page', $page);

        $nickname = !empty($post['nickname'])?$post['nickname']:'';

        $this->assign('nickname',$nickname);

        $this->assign('res', $res);
        return $this->fetch();        
    }

    // 用户列表 - 普通人员 - 编辑
    public function user_list_info_edit($id)
    {
        $post = Request::instance()->param();
        if ($_POST) {
            $data['member_status'] = $post['member_status'];
            $res = db('user')->where(['id'=>$id])->update($data);
            if ($res) {
                $data['status'] = 1;
                $data['info'] = '编辑成功';
                $data['url'] = url('admin/xixiche/user_list_info_edit', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }else{
                $data['status'] = 0;
                $data['info'] = '编辑失败';
                $data['url'] = url('admin/xixiche/user_list_info_edit', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }
        }
        $info = db('user')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    // 用户列表 - 洗车师傅 - 编辑
    public function user_list_edit($id)
    {
        $post = Request::instance()->param();       
        if ($_POST) {
            $data['nickname'] = $post['nickname'];
            $data['gender'] = $post['gender'];
            $data['mobile'] = $post['mobile'];
            $data['account'] = $post['account'];
            $data['password'] = $post['password'];
            $data['grade'] = $post['grade'];
            $data['member_status'] = $post['member_status'];
            $file = new \app\admin\logic\File();
            
            $data['avatarurl'] = $file->uploadFileAdminPro($this->accountId);
            if (empty($data['avatarurl'])) {
                unset($data['avatarurl']);
            }
            $res = db('user')->where(['id'=>$id])->update($data);
            if ($res) {
                $data['status'] = 1;
                $data['info'] = '编辑成功';
                $data['url'] = url('admin/xixiche/user_list_edit', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }else{
                $data['status'] = 0;
                $data['info'] = '编辑失败';
                $data['url'] = url('admin/xixiche/user_list_edit', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }
        }
        $info = db('user')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        return $this->fetch();


    }
    
    // 用户列表 - 删除
    public function user_list_info_del($id)
    {
        $res = db('user')->where(['id'=>$id])->update(['member_status'=>0]);
        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/user_list_info';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/user_list_info';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }

    // 订单列表 - 预约到店
    public function order_list_zero()
    {
        $post = Request::instance()->param();
        $where['admin_id'] = $this->accountId;
        $where['status'] = 1;
        $where['pay_type'] = 3;
        $where['order_type'] = 2;
        if ($_GET) {
            if (!empty($post['order_id'])) {
                $where['order_id'] = ['like','%'.$post['order_id'].'%'];
            }
        }
        $res = db('xixiche_order')->where($where)->order('id desc')->paginate(20,false);
        
        $res->toArray();
        foreach ($res as $key => $val){
            $data = [];
            $data = $val;
            $info = db('user')->where(['id'=>$val['user_info_id']])->find();
            $data['user_info_id'] = $info['nickname'];
            $res -> offsetSet($key,$data);
        }
        $page = $res->render();
        $this->assign('page', $page);

        $order_id = !empty($post['order_id'])?$post['order_id']:'';
        $this->assign('order_id',$order_id);

        $this->assign('res', $res);
        return $this->fetch();
    }

    // 订单列表 - 购买月卡
    public function order_list_member()
    {
        $post = Request::instance()->param();
        $where['admin_id'] = $this->accountId;
        $where['pay_type'] = 2;
        $where['order_type'] = 1;
        $where['status'] = 1;
        if ($_GET) {
            if (!empty($post['order_id'])) {
                $where['order_id'] = ['like','%'.$post['order_id'].'%'];
            }
        }
        $res = db('xixiche_order')->where($where)->order('id desc')->paginate(20,false);
        $res->toArray();
        foreach ($res as $key => $val) {
            $data = [];
            $data = $val;
            $info = db('user')->where(['id'=>$val['user_id']])->find();
            $data['user_id'] = $info['nickname'];
            $res-> offsetSet($key, $data);
        }
        $page = $res->render();
        $this->assign('page', $page);
        $order_id = !empty($post['order_id'])?$post['order_id']:'';
        $this->assign('order_id',$order_id);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 订单列表 - 预约洗车
    public function order_list_one()
    {
        $post = Request::instance()->param();
        $where['admin_id'] = $this->accountId;
        $where['status'] = 1;
        $where['order_type'] = 2;
        $where['pay_type'] = [['=','1'],['=','2'],'or'];
        if ($_GET) {
            if (!empty($post['order_id'])) {
                $where['order_id'] = ['like','%'.$post['order_id'].'%'];
            }
        }
        $res = db('xixiche_order')->where($where)->order('id desc')->paginate(20,false);
        $res->toArray();
        foreach ($res as $key => $val){
            $data = [];
            $data = $val;
            $info = db('user')->where(['id'=>$val['user_info_id']])->find();
            $data['user_info_id'] = $info['nickname'];
            $res -> offsetSet($key,$data);
        }
        $page = $res->render();
        $this->assign('page', $page);
        $order_id = !empty($post['order_id'])?$post['order_id']:'';
        $this->assign('order_id',$order_id);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 订单列表 - 分配技师
    public function order_list_info($id)
    {
        $post = Request::instance()->param();

        if ($_POST) {
            $data['user_info_id'] = $post['user_info_id'];
            $data['status'] = 2;
            $data['going_time'] = time();
            $res = db('xixiche_order')->where(['id'=>$id])->update($data);
            if ($res) {
                $data['status'] = 1;
                $data['info'] = '分配成功';
                $data['url'] = url('admin/xixiche/order_list_info', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }else{
                $data['status'] = 0;
                $data['info'] = '分配失败';
                $data['url'] = url('admin/xixiche/order_list_info', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }
        }
        $user = db('xixiche_order')->where(['id'=>$id])->find();

        $info = db('user')->where(['member_status'=>1,'admin_id'=>$this->accountId])->select();
        $this->assign('info', $info);
        $this->assign('user', $user);
        return $this->fetch();
    }

    // 订单列表 - 安排技师前往
    public function order_list_two()
    {
        $post = Request::instance()->param();
        $where['admin_id'] = $this->accountId;
        $where['status'] = 2;
        $where['order_type'] = 2;
        $where['pay_type'] = [['=','1'],['=','2'],'or'];
        if ($_GET) {
            if (!empty($post['order_id'])) {
                $where['order_id'] = ['like','%'.$post['order_id'].'%'];
            }
        }
        $res = db('xixiche_order')->where($where)->order('id desc')->paginate(20,false);
        $res->toArray();
        foreach ($res as $key => $val){
            $data = [];
            $data = $val;
            $info = db('user')->where(['id'=>$val['user_info_id']])->find();
            $data['user_info_id'] = $info['nickname'];
            $res -> offsetSet($key,$data);
        }
        $page = $res->render();
        $this->assign('page', $page);
        $order_id = !empty($post['order_id'])?$post['order_id']:'';
        $this->assign('order_id',$order_id);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 订单列表 - 洗车前
    public function order_list_three()
    {
        $post = Request::instance()->param();
        $where['admin_id'] = $this->accountId;
        $where['status'] = 3;
        $where['order_type'] = 2;
        $where['pay_type'] = [['=','1'],['=','2'],'or'];
        if ($_GET) {
            if (!empty($post['order_id'])) {
                $where['order_id'] = ['like','%'.$post['order_id'].'%'];
            }
        }
        $res = db('xixiche_order')->where($where)->order('id desc')->paginate(20,false);

        $res->toArray();
        foreach ($res as $key => $val){
            $data = [];
            $data = $val;
            $info = db('user')->where(['id'=>$val['user_info_id']])->find();
            $data['user_info_id'] = $info['nickname'];
            $res -> offsetSet($key,$data);
        }
        $page = $res->render();
        $this->assign('page', $page);
        $order_id = !empty($post['order_id'])?$post['order_id']:'';
        $this->assign('order_id',$order_id);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 订单列表 - 洗车后
    public function order_list_four()
    {
        $post = Request::instance()->param();
        $where['admin_id'] = $this->accountId;
        $where['status'] = 4;
        $where['pay_type'] = [['=','1'],['=','2'],'or'];
        if ($_GET) {
            if (!empty($post['order_id'])) {
                $where['order_id'] = ['like','%'.$post['order_id'].'%'];
            }
        }
        $res = db('xixiche_order')->where($where)->order('id desc')->paginate(20,false);
        $res->toArray();
        foreach ($res as $key => $val){
            $data = [];
            $data = $val;
            $info = db('user')->where(['id'=>$val['user_info_id']])->find();
            $data['user_info_id'] = $info['nickname'];
            $res -> offsetSet($key,$data);
        }        
        $page = $res->render();
        $this->assign('page', $page);
        $order_id = !empty($post['order_id'])?$post['order_id']:'';
        $this->assign('order_id',$order_id);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 订单删除
    public function order_del_one($id)
    {
        $res = db('xixiche_order')->where(['id'=>$id])->update(['status'=>6]);

        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/order_list_one';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/order_list_one';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }
    // 订单删除
    public function order_del_member($id)
    {
        $res = db('xixiche_order')->where(['id'=>$id])->update(['status'=>7]);

        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/order_list_member';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/order_list_member';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }
    // 订单删除
    public function order_del_two($id)
    {
        $res = db('xixiche_order')->where(['id'=>$id])->update(['status'=>6]);

        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/order_list_two';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/order_list_two';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }
    // 订单删除
    public function order_del_three($id)
    {
        $res = db('xixiche_order')->where(['id'=>$id])->update(['status'=>6]);

        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/order_list_three';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/order_list_three';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }
    // 订单删除
    public function order_del_four($id)
    {
        $res = db('xixiche_order')->where(['id'=>$id])->update(['status'=>6]);

        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/order_list_four';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/order_list_four';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }

    // 订单删除
    public function order_del_zero($id)
    {
        $res = db('xixiche_order')->where(['id'=>$id])->update(['status'=>6]);

        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/order_list_zero';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/order_list_zero';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }

    // 项目管理 - 服务内容 - 展示
    public function project_list()
    {
        $where['admin_id'] = $this->accountId;
        $res = db('xixiche_project')->where($where)->select();

        $this->assign('res', $res);
        return $this->fetch();
    }

    // 项目管理 - 服务内容 - 添加
    public function project_list_add()
    {
        $post = Request::instance()->param();
        if ($_POST) {
            $data['title'] = $post['title'];
            $file = new \app\admin\logic\File();
            $data['thumb'] = $file->uploadFileAdmin($this->accountId);
            $data['money'] = $post['money'];
            $data['status'] = $post['status'];
            $data['sort'] = $post['sort'];
            $data['admin_id'] = $this->accountId;
            $res = db('xixiche_project')->insert($data);
            if ($res) {
                $data['status'] = 1;
                $data['info'] = '添加成功';
                $data['url'] = 'admin/xixiche/project_list';
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }else{
                $data['status'] = 0;
                $data['info'] = '添加失败';
                $data['url'] = 'admin/xixiche/project_list';
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }
        }

        return $this->fetch();
    }

    // 项目管理 - 服务内容 - 编辑
    public function project_list_edit($id)
    {
        $post = Request::instance()->param();
        if ($_POST) {
            $data['title'] = $post['title'];
            $file = new \app\admin\logic\File();
            $data['thumb'] = $file->uploadFileAdmin($this->accountId);
            if (empty($data['thumb'])) {
                unset($data['thumb']);
            }
            $data['money'] = $post['money'];
            $data['status'] = $post['status'];
            $data['sort'] = $post['sort'];
            $res = db('xixiche_project')->where(['id'=>$id])->update($data);
            if ($res) {
                $data['status'] = 1;
                $data['info'] = '编辑成功';
                $data['url'] = url('admin/xixiche/project_list_edit',['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }else{
                $data['status'] = 0;
                $data['info'] = '编辑失败';
                $data['url'] = url('admin/xixiche/project_list_edit',['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }
        }

        $info = db('xixiche_project')->where(['id'=>$id])->find();
        $this->assign('info', $info);

        return $this->fetch();
    }

    // 项目管理 - 服务内容 - 删除
    public function project_list_del($id)
    {
        $res = db('xixiche_project')->where(['id'=> $id])->delete();
        if ($res) {
            $data['status'] = 1;
            $data['info'] = '删除成功';
            $data['url'] = 'admin/xixiche/project_list';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }else{
            $data['status'] = 0;
            $data['info'] = '删除失败';
            $data['url'] = 'admin/xixiche/project_list';
            $this->assign('data', $data);
            return $this->fetch('xixiche/statuspage');
        }
    }

    // 项目管理 - 会员卡 - 展示
    public function member_list()
    {
        $where['admin_id'] = $this->accountId;
        $res = db('xixiche_member')->where($where)->select();

        $this->assign('res', $res);
        return $this->fetch();
    }

    // 项目管理 - 会员卡 - 编辑
    public function member_list_edit($id)
    {
        $post = Request::instance()->param();
        if ($_POST) {
            $data['title'] = $post['title'];
            $data['money'] = $post['money'];
            $data['num'] = $post['num'];
            $data['time'] = $post['time'];

            $res = db('xixiche_member')->where(['id'=>$id])->update($data);
            if ($res) {
                $data['status'] = 1;
                $data['info'] = '编辑成功';
                $data['url'] = url('admin/xixiche/member_list_edit',['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }else{
                $data['status'] = 0;
                $data['info'] = '编辑失败';
                $data['url'] = url('admin/xixiche/member_list_edit',['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }
        }
        $info = db('xixiche_member')->where(['id'=>$id])->find();

        $this->assign('info', $info);
        return $this->fetch();
    }

    // 公司管理 - 编辑
    public function company_addr()
    {
        $post = Request::instance()->param();
        if ($_POST) {
            $data['address'] = $post['address'];
            $data['longitude'] = $post['longitude'];
            $data['latitude'] = $post['latitude'];
            $data['mobile'] = $post['mobile'];
            $res = db('xixiche_company')->where(['id'=>1])->update($data);
            if ($res) {
                $data['status'] = 1;
                $data['info'] = '编辑成功';
                $data['url'] = url('admin/xixiche/company_addr',['id'=>1]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }else{
                $data['status'] = 0;
                $data['info'] = '编辑失败';
                $data['url'] = url('admin/xixiche/company_addr',['id'=>1]);
                $this->assign('data', $data);
                return $this->fetch('xixiche/statuspage');
            }
        }
        $info = db('xixiche_company')->where(['id'=>1])->find();

        $this->assign('info', $info);
        return $this->fetch();
    }

    // 评价管理
    public function comment_list()
    {
        $where['admin_id'] = $this->accountId;
        if($_GET)
        {
            if(!empty($post['order_id'])){
                $where['order_id'] = ['like','%'.$post['order_id'].'%'];
            }
        }         
        $res = db('xixiche_comment')->where($where)->order('id desc')->paginate(20,false);

        $res->toArray();
        foreach ($res as $key => $val){
            $data = [];
            $data = $val;
            $info = db('user')->where(['id'=>$val['user_info_id']])->find();
            $data['user_info_id'] = $info['nickname'];
            $infos = db('user')->where(['id'=>$val['user_id']])->find();
            $data['user_id'] = $infos['nickname'];
            $inf = db('xixiche_order')->where(['id'=>$val['order_id']])->find();
            $data['order_id'] = $inf['order_id'];
            $res -> offsetSet($key,$data);
        }

        $page = $res->render();

        $this->assign('res', $res);
        $this->assign('page', $page);

        return $this->fetch();
    }
       
    
}